package org.xiaohanghu.starter.net;

import java.net.MalformedURLException;
import java.net.URI;
import java.net.URISyntaxException;
import java.net.URL;

/**
 * @author xiaohanghu
 */
public class HomeUrlConvertor implements UrlConvertor {

	public static final String URI_SCHEME = "home";

	private URI homeUri;

	public HomeUrlConvertor(URI homeUri) {
		this.homeUri = homeUri;
	}

	@Override
	public URL convert(URI uri) throws MalformedURLException,
			URISyntaxException {
		String scheme = homeUri.getScheme();
		String homePath = homeUri.getPath();
		String path = uri.getPath();
		path = homePath + "/" + path;
		// path = homeUri + path;
		String userInfo = uri.getUserInfo();
		String host = uri.getHost();
		int port = uri.getPort();
		String query = uri.getQuery();
		String fragment = uri.getFragment();

		return new URI(scheme, userInfo, host, port, path, query, fragment)
				.toURL();
	}

}
